<?php
  // no direct access
  defined( '_JEXEC' ) or die( 'Restricted access' );
  jimport('joomla.application.component.controller');
  include('base.php');

  class GameStatController extends BaseController
  {
    var $_name = 'gamestat';
    
    function display()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      
      $view->displayList();
    }
    
    function add()
    {
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel('schedule','schedulemodel'));
      $view->setModel($this->getModel('gameendstatus','gameendstatusmodel'));
      
      $view->displayAdd();
    }
    
    function edit()
    {
      $cids = JRequest::getVar('cid', null, 'default', 'array' ); 
      if($cids === null)
          JError::raiseError(500, 'cid parameter missing from the request');
      
      $document =& JFactory::getDocument();
      
      $view = & $this->getView($this->_name, $document->getType());
      $view->setModel($this->getModel($this->_name), true);
      $view->setModel($this->getModel('team','teammodel'));
      $view->setModel($this->getModel('schedule','schedulemodel'));
      $view->setModel($this->getModel('gameendstatus','gameendstatusmodel'));
      
      $view->displayEdit();  
    }
    
    
    function ajx_select_scheds()
    {
      $team_id     = JRequest::getVar('team_id');
      $schedule    = $this->getModel('schedule','schedulemodel');
      $team        = $this->getModel('team','teammodel');
      
      $result      = array();
      $schedules   = $schedule->get_by_team_id($team_id);
      if($schedules){
        foreach($schedules as $schedule){
          $old_data           = $schedule->schedule;
          $team1              = $team->get($schedule->first_team_id)->name;
          $team2              = $team->get($schedule->second_team_id)->name;
          $schedule->schedule = "$old_data   $team1 vs. $team2";
        }
        $result = $schedules;
      }
      $html_attrib = "size=20 onchange=\"ajx_request('". JRequest::getVar('c', 'gamestat') ."', 'ajx_select_sched_details', 'schedule_id='+this.value , 'div_teams');\"";  
      $result  = JHTML::_('select.genericlist', $result, 'schedule_id', $html_attrib, 'id', 'schedule', null);
      echo $result;
    }
    
    function ajx_select_sched_details()
    {
      $schedule_id     = JRequest::getVar('schedule_id');
      $mschedule        = $this->getModel('schedule','schedulemodel');
      $mteam            = $this->getModel('team','teammodel');
      
      $teams      = array();
      $schedule   = $mschedule->get($schedule_id);
      
      if($schedule){
        $first_team_id  = $schedule->first_team_id;
        $second_team_id = $schedule->second_team_id;
        
        $teams[$first_team_id]  = $mteam->get($schedule->first_team_id);
        $teams[$second_team_id] = $mteam->get($schedule->second_team_id);
        
        $result  = JText::_($teams[$first_team_id]->name);
        $result .= "<input type='text' name='first_team_final_score' id='first_team_final_score' value='0' />";
        $result .= JText::_(' VS. ');
        $result .= JText::_($teams[$second_team_id]->name);
        $result .= "<input type='text' name='second_team_final_score' id='second_team_final_score' value='0' />";
        echo $result;
      }
      
    }
  }
?>
